<?php

!defined('IN_HDWIKI') && exit('Access Denied');

class commentmodel {

	var $db;
	var $base;

	function commentmodel(&$base) {
		$this->base = $base;
		$this->db = $base->db;
	}

	function get_comments($did,$start=0,$limit=10){
		$comments=array();
		$query=$this->db->query("SELECT * FROM ".DB_TABLEPRE."comment WHERE did='$did' ORDER BY `time` DESC LIMIT $start,$limit");
		while($comment=$this->db->fetch_array($query)){
			$comment['time']=$this->base->date($comment['time']);
			$comments[]=$comment;
		}
		return $comments;
	}
	
	function remove_comment_by_id($id){
		if(is_array($id)){
			$ids = @implode(',',$id);
			$sql="DELETE FROM ".DB_TABLEPRE."comment WHERE id IN ($ids) ";
		}else{
			$sql="DELETE FROM ".DB_TABLEPRE."comment WHERE id =$id";
		}
		return $this->db->query($sql);
	}
	
	function add_comment($did,$comment){
		$this->db->query("INSERT INTO ".DB_TABLEPRE."comment(did,comment,author,authorid,time) VALUES($did,'$comment','".$this->base->user['username']."',".$this->base->user[uid].",".$this->base->time.") ");
		return $this->db->insert_id();
	}
	
	function edit_comment_by_id($id,$comment){
		$this->db->query("UPDATE ".DB_TABLEPRE."comment SET comment='$comment' WHERE id=$id");
	}
	
	function search_comment_num($cid='', $did='',$author='', $starttime='', $endtime='' ){
		$sql="SELECT  count(*)  FROM ".DB_TABLEPRE."comment m LEFT JOIN ".DB_TABLEPRE."doc d ON d.did = m.did WHERE 1=1 ";
		if($cid){
			$query=$this->db->query("SELECT did FROM ".DB_TABLEPRE."doc  WHERE cid = $cid");
			while($doc=$this->db->fetch_array($query)){
				$id[]=$doc['did'];
			}
			$dids=@implode(',',$id);
			$sql=$sql." AND m.did IN ($dids) ";
		}
		if($did){
			$sql=$sql." AND m.did ='$did' ";
		}
		if($author){
			$sql=$sql." AND m.author='$author' ";
		}
		if($starttime){
			$sql=$sql." AND m.time>=$starttime ";
		}
		if($endtime){
			$sql=$sql." AND m.time<=$endtime ";
		}
		return $this->db->result_first($sql);
	}
	
	function search_comment($start=0,$limit=10, $cid='', $did='',$author='', $starttime='', $endtime='' ){
		$commentlist=array();
		$sql="SELECT  m.id,m.did,m.comment,m.author,m.authorid,m.time,d.title title FROM ".DB_TABLEPRE."comment m LEFT JOIN ".DB_TABLEPRE."doc d ON d.did = m.did WHERE 1=1 ";
		if($cid){
			$query=$this->db->query("SELECT did FROM ".DB_TABLEPRE."doc  WHERE cid = $cid");
			while($doc=$this->db->fetch_array($query)){
				$id[]=$doc['did'];
			}
			$dids=@implode(',',$id);
			$sql=$sql." AND m.did IN ($dids) ";
		}
		if($did){
			$sql=$sql." AND m.did ='$did' ";
		}
		if($author){
			$sql=$sql." AND m.author='$author' ";
		}
		if($starttime){
			$sql=$sql." AND m.time>=$starttime ";
		}
		if($endtime){
			$sql=$sql." AND m.time<=$endtime ";
		}
		$sql=$sql." ORDER BY m.time DESC LIMIT $start,$limit ";
		$query=$this->db->query($sql);
		while($comment=$this->db->fetch_array($query)){
			$comment['time'] = $this->base->date($comment['time']);
			$comment['partcomment']=(strlen($comment['comment'])>28)?string::substring($comment['comment'],0,28)."......":$comment['comment'];
			$commentlist[]=$comment;
		}
		return $commentlist;
	}
	
	function recent_comment($start=0,$limit=10){
		$comments=array();
		$query=$this->db->query('SELECT * FROM '.DB_TABLEPRE.'comment  ORDER BY `time` DESC LIMIT '."$start,$limit");
		while($comment=$this->db->fetch_array($query)){
			$comment['tipcomment']=(string::hstrlen($comment['comment'])>12)?string::substring($comment['comment'],0,12)."...":$comment['comment'];
			$comment['time']=$this->base->date($comment['time']);
			$comments[]=$comment;
		}
		return $comments;
	}
}
?>